package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import view.actionlisteners.SpeicherButtonActionListener;

import model.Address;

@SuppressWarnings("serial")
public class AddressView extends JFrame {

	public Address address;

	public AddressListView addressListView;

	private JTextField nameTextField;
	private JTextField emailaddressTextField;

	public AddressView(Address address, AddressListView addressListView) {

		this.address = address;
		this.addressListView = addressListView;

		init();

		populateFields();

		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		this.setVisible(true);
	}

	private void init() {
		this.setTitle("Address");
		this.setLayout(new BorderLayout());

		JPanel upperPanel = new JPanel();
		upperPanel.setBorder(BorderFactory
				.createTitledBorder("Addressinformationen"));

		upperPanel.setLayout(new GridLayout(2, 2, 5, 5));

		JLabel nameLabel = new JLabel("Name");
		nameTextField = new JTextField();

		JLabel emailaddressLabel = new JLabel("Emailadresse");
		emailaddressTextField = new JTextField();

		upperPanel.add(nameLabel);
		upperPanel.add(nameTextField);
		upperPanel.add(emailaddressLabel);
		upperPanel.add(emailaddressTextField);

		this.add(upperPanel, BorderLayout.CENTER);

		JButton speicherButton = new JButton("Hinterlegen");

		speicherButton
				.addActionListener(new SpeicherButtonActionListener(this));

		this.add(speicherButton, BorderLayout.SOUTH);

		this.pack();
	}

	private void populateFields() {
		nameTextField.setText(address.getName());
		emailaddressTextField.setText(address.getEmailaddress());
	}

	public void retrieveFields() {
		address.setName(nameTextField.getText());
		address.setEmailaddress(emailaddressTextField.getText());
	}

}
